<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page  import="java.util.*"%>
<%@page  import="java.sql.Connection"%>
<%@page  import="java.sql.DriverManager"%>
<%@page  import="com.freework.util.SpringBeanUtil"%>

<%@page  import="java.sql.Statement"%>
<%@page  import="java.sql.ResultSet"%>
<%@page  import="java.sql.ResultSetMetaData"%>
<%@page  import="java.sql.SQLException"%>
<%
 
String sql=request.getParameter("sql");
sql="select * from ( "+sql+" ) table_tmp_app_test where 2=1";

Connection con = null;
Statement st =null;
try {
javax.sql.DataSource  dataSource=(javax.sql.DataSource)SpringBeanUtil.getBeanByName("dataSource");
con=dataSource.getConnection();
System.out.println(con);
st = con.createStatement();
 
ResultSetMetaData data = st.executeQuery(sql).getMetaData();
out.print("[");

int count=data.getColumnCount();
for ( int i = 1 ; i <=count;i++){
	String colname=data.getColumnName(i);
	out.print("{");
	out.print("name:'");out.print(colname.toLowerCase());out.print("'");
	if(Number.class.isAssignableFrom(Class.forName(data.getColumnClassName(i)))){
		out.print(",type:'number'");
	}else{
		
		out.print(",type:'string'");
	}
	
	out.print("}");
	if(count!=i)
	out.print(",");
}
out.print("]");
} catch (SQLException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
	response.setStatus(500);
	
	out.print(e.getMessage());
	//response.sendError(500,e.getMessage());
	
}finally{
	if(st!=null)
		try {
			st.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	if(con!=null)
		try {
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	
	
}
//response.flushBuffer();
//out.close();
%>